package com.alstudio.net.protocol.xmpp.parser.message;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.text.TextUtils;

import com.alstudio.net.protocol.xmpp.XmppListener;
import com.alstudio.net.protocol.xmpp.packact.Message;
import com.alstudio.net.protocol.xmpp.parser.BaseParser;
import com.alstudio.utils.log.Mylog;
import com.alstudio.utils.xml.BaseXmlParser;
import com.alstudio.utils.xml.BaseXmlParser.XmlParseCallback;

public class MessageParser extends BaseParser implements XmlParseCallback {

	private Message msg;
	private MessageXmlParser mParser;
	private String type;

	public MessageParser(Message msg, String stream, XmppListener listener) {
		// TODO Auto-generated constructor stub
		this.stream = stream;
		this.msg = null;
		this.type = null;
		this.msg = msg;
		type = msg.getMsgType();
		mListener = listener;
		try {
			mParser = new MessageXmlParser(stream, this);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected class MessageXmlParser extends BaseXmlParser {

		protected MessageXmlParser(String xml, XmlParseCallback callback)
				throws XmlPullParserException {
			// TODO Auto-generated constructor stub
			createParser(xml);
			setXmlParseCallback(callback);
		}

		protected void start() throws XmlPullParserException, IOException {
			startParse();
		}

	}

	public void startParseMessage() {
		Mylog.d("start parsing message...");

		try {
			mParser.start();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Mylog.d("message parse done!");
	}

	@Override
	public void onTagStart(String tag) {
		// TODO Auto-generated method stub
		Mylog.d("节点开始了: " + tag);

		try {
			if (tag.equals("fromnick")) {
				msg.setFromNick(mParser.readText());
			} else if (tag.equals("body")) {
				msg.setBody(mParser.readText());
			} else if (tag.equals("id")) {
				msg.setMsgId(mParser.readText());
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void onTagEnd(String tag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDocumentStart() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDocumentEnd() {
		// TODO Auto-generated method stub
		// 解析完成，回调通知
		onCallback();
	}

	private void onCallback() {
		if (!TextUtils.isEmpty(type) && !type.equals("error")) {
			if (mListener != null) {
				mListener.onJxaMessageEvent(msg);
			}
		} else {
			if (mListener != null) {
				// 回执来的，不需要发回执,直接通知
				mListener.onJxaGetMessageBackEvent(msg.getFrom(),
						msg.getMsgId());
			}
		}
	}

}
